<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/global.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/security_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/account_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/level_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/page_navigator.php");

	SecurityMod::login();

	$level = $_SESSION["account"]->getLevel();
	if ($level != "Admin")
		SecurityMod::logout();

	$pageNumber = intval(v($_REQUEST, "page"));
	if ($pageNumber < 1)
		$pageNumber = 1;

	function drawNavigator() {
		global $pageCount, $pageNumber;
		$params = "";
		PageNavigator::draw($pageCount, $pageNumber, 33, $params, "accounts.php");
	}

	$accounts = AccountMod::getAccounts($pageNumber, $pageCount);

	$levels = LevelMod::getLevels();
        $chatLevels = AccountMod::getChatLevels();
        $pwd = SecurityMod::generatePassword();
?>
<html>
<head>
    <script language="JavaScript" type="text/javascript">
    function showDetail(pid){
        var leftPos = 0;
        var topPos = 0;
        if (screen) {
            leftPos = (screen.width / 2) - 275;
            topPos = (screen.height / 2) - 390;
        }
        window.open("user_details.php?pid="+pid, "_blank", "width=550,height=780,scrollbars=1,resizable=1,left=" + leftPos + ",top=" + topPos);
}
</script>
<title><?php echo(SettingsMod::PAGE_TITLE." :: Accounts"); ?></title>
<link rel="stylesheet" href="main.css">
</head>
<body>
	<h2 align="center">Account List</h2>
	<table align="center" width="900">
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<td colspan="10"><?php drawNavigator()?></td>
			</tr>
			<tr>
				<th>&nbsp;</th>
                                <th><u>ID</u></th>
				<th><u>Name</u></th>
				<th><u>Universe</u></th>
				<th><u>Security</u></th>
				<th><u>Permissions</u></th>
				<th><u>Pilot ID</u></th>
				<th><u>Password</u></th>
                                <th><u>Validation</u></th>
                                <th><u>Details</u></th>
				<th><u>Delete</u></th>
			</tr>
			<?php
				$i = 0;
				foreach ($accounts as $acc):
				$i++;
			?>
			<tr bgcolor='#0b0b2f'>
				<td align='right' style='cursor:crosshair'>
					<?php echo(($pageNumber - 1) * SettingsMod::PAGE_RECORDS_PER_PAGE + $i)?>.
				</td>
                                <td><?php echo($acc->getPilotId()); ?></td>
				<td><?php $acc->drawName(); ?></td>
				<td style="text-align: center">
					<?php echo($acc->getUniverse()); ?>
				</td>

				<td style="text-align: center">
					<?php echo $acc->getLevel(); ?>
				</td>

				<!-- Permissions -->
				<form method="post" action="account_permissions.php">
				<td style="text-align: center">
					<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
					<input type="submit" value="Permissions" />
				</td>
				</form>
				
				<!-- Pilot ID -->
				<form method="post" action="account_pid.php">
				<td style="text-align: center">
					<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
					<input type="submit" value="Pilot ID" />
				</td>
				</form>

				<!-- Password -->
				<form method="post" action="account_password.php">
				<td style="text-align: center">
					<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
					<input type="submit" value="Password" />
				</td>
				</form>
                                
				<!-- Validation -->
				<form method="post" action="account_validation.php">
				<td style="text-align: center">
                                    <input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
					<input type="submit" value="Validation" />
				</td>
				</form>
                                
                                <!-- Details -->
                                <td style="text-align: center">
                                    <input type="button" onClick="showDetail(<?php echo($acc->getPilotId()); ?>)" value="Details" />
                                </td>

				<!-- Delete -->
				<form action="account_delete.php" method="get">
				<td style="text-align: center">
						<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
						<input type="submit" value="Delete" />
				</td>
				</form>
			</tr>
			<?php endforeach; ?>
			<tr>
				<td colspan="99"><?php drawNavigator()?></td>
			</tr>
		</td>
	</tr>
	</table>

	<br />
	<h2 align="center">Create Account</h2>
	<form method="post" action="account_add.php">
	<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center">
	<tr>
		<th>Username:</th>
		<td><input type="text" name="name" style="width: 100%" /></td>
	</tr>
	<tr>
		<th>Pilot ID:</th>
		<td><input type="text" name="pid" style="width: 100%" /></td>
	</tr>
	<tr>
		<th>Password:</th>
		<td><input type="text" name="password" style="width: 100%" value="<?php print($pwd); ?>" /></td>
	</tr>
	<tr>
		<th>Password Confirm:</th>
		<td><input type="text" name="password_confirm" style="width: 100%" value="<?php print($pwd); ?>" /></td>
	</tr>
	<tr>
		<th>Validation:</th>
		<td><input type="text" name="validation" style="width: 100%" value="<?php print(uniqid('', true)); ?>" /></td>
	</tr>
	<tr>
		<th>Chat Level:</th>
		<td>
		<select name="chatLevel" style="width: 100%">
		<?php foreach($chatLevels as $key=>$value): ?>
			<option value="<?php echo($key); ?>"><?php echo($chatLevels[$key]['Name']); ?></option>
		<?php endforeach; ?>
		</select>
		</td>
	</tr>
	<tr>
		<th>Security Level:</th>
		<td>
		<select name="level" style="width: 100%">
		<?php foreach($levels as $level): ?>
			<option><?php echo($level->getName()); ?></option>
		<?php endforeach; ?>
		</select>
		</td>
	</tr>
	<tr>
		<th>Basic script access:</th>
		<td><input type="checkbox" name="BASIC" checked="checked" /></td>
	</tr>
	<tr>
		<th>Advanced script access:</th>
		<td><input type="checkbox" name="ADVANCED" /></td>
	</tr>
	<tr>
		<th>Admin script access:</th>
		<td><input type="checkbox" name="ADMIN" /></td>
	</tr>
	<tr>
		<th>PFC SB script access:</th>
		<td><input type="checkbox" name="PFCSB" /></td>
	</tr>
	<tr>
		<th>Secret script access:</th>
		<td><input type="checkbox" name="SECRET" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Create Account" style="width: 100%" /></td>
	</tr>
	</table>
	</form>
</body>
</html>
